package com.jiandaoshou;

import android.os.Handler;
import android.os.Message;
import android.widget.ListView;

import com.andview.refreshview.XRefreshView;
import com.jiandaoshou.adapter.DingDanAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.DingDanDetailEntry;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.JsonUtil;
import com.jiandaoshou.utils.TimeUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

public class MyMessageAct extends BaseActivity {
    private ListView mListView;
    private XRefreshView xRefreshView;
    private DingDanAdapter adapter;
    private List<DingDanDetailEntry> datas;
    private String shopID;
    private int page = 1, count = 10;
    private String starttime, endtime;
    //true下拉加载
    private boolean flag = true;
    private long lastRefreshTime;

    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_mymessage;
    }

    @Override
    protected String getTitleText() {
        return "我的消息";
    }

    @Override
    public void init() {
        mListView = $(R.id.lv_mymessage);
        xRefreshView = $(R.id.listview_refresh);
        datas = new ArrayList<DingDanDetailEntry>();
        lastRefreshTime = System.currentTimeMillis();
        // 设置是否可以下拉刷新
        xRefreshView.setPullRefreshEnable(false);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(false);
        // 设置上次刷新的时间
        xRefreshView.restoreLastRefreshTime(lastRefreshTime);
        // 设置时候可以自动刷新
        xRefreshView.setAutoRefresh(false);
        xRefreshView.setHideFooterWhenComplete(false);
        adapter = new DingDanAdapter(this, datas);
        mListView.setAdapter(adapter);
        //加载数据
        GetData(page,0);
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        flag = true;
                        GetData(page,1);
                    }
                });
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        flag = false;
                        GetData(page,2);
                    }
                });
            }

            @Override
            public void onRelease(float direction) {
                super.onRelease(direction);
                //上拉
                if (direction > 0) {
                }
                //下拉
                else {
                }
            }
        });
    }

    //网络请求
    private void GetData(int page, final int type) {
        //获取网络请求的数据
        shopID = getIntent().getStringExtra("shopId");
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        starttime = TimeUtil.getInstance().getMonthBeginTime(year, month);
        endtime = TimeUtil.getInstance().getMonthEndTime(year, month);
        HashMap<String, String> map = new HashMap<>();
        map.put("strEndTime", endtime);
        map.put("strBeginTime", starttime);
        map.put("strShopID", shopID);
        map.put("strPayMode", "");
        map.put("order", "desc");
        map.put("strShopSonName", "");
        map.put("sort", "dtTransactionTime");
        map.put("rows", count + "");
        map.put("page", page + "");
        Http.post(APIDefine.TF_Interface_GetOrderMsg, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    JSONArray array = (JSONArray) object.get("OrderList");
                    DingDanDetailEntry entry;
                    for (int i = 0; i < array.length(); i++) {
                        entry = JsonUtil.parser.fromJson(array.get(i).toString(), DingDanDetailEntry.class);
                        datas.add(entry);
                    }
                    adapter.notifyDataSetChanged();
                    if (type == 1) {
                        xRefreshView.stopRefresh();
                    } else {
                        xRefreshView.stopLoadMore();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
